﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminCategories : Form
    {
        private aDbDataSet.CategoriesRow drActiveCategory;
        private bool bNew;
        
        public frmAdminCategories()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Load
        private void frmAdminCategories_Load(object sender, EventArgs e)
        {
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;
            this.categoriesTableAdapter.Fill(this.aDbDataSet.Categories);
        }

        // Selection changed
        private void categoriesDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (categoriesDataGridView.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)categoriesDataGridView.CurrentRow.DataBoundItem;
                chkActive.Checked = Convert.ToBoolean(drv["Active"]);
                txtName.Text = drv["Name"].ToString();
                txtAlterName.Text = drv["AlterName"].ToString();
                txtCodeName.Text = drv["CodeName"].ToString();
                txtSex.Text = drv["Sex"].ToString();
                nudYearFrom.Value = Convert.ToInt32(drv["YearFrom"]);
                nudYearTo.Value = Convert.ToInt32(drv["YearTo"]);
                txtPoints.Text = drv["Points"].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                chkActive.Checked = true;
                txtName.Text = string.Empty;
                txtAlterName.Text = string.Empty;
                txtSex.Text = string.Empty;
                nudYearFrom.Value = 0;
                nudYearTo.Value = 0;
                txtPoints.Text = string.Empty;

                this.drActiveCategory = aDbDataSet.Categories.NewCategoriesRow();

                this.drActiveCategory["Active"] = true;
                this.drActiveCategory["Sync"] = false;
                this.drActiveCategory["WebId"] = 0;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;
                
                DataRowView drv = (DataRowView)categoriesDataGridView.CurrentRow.DataBoundItem;
                this.drActiveCategory = (aDbDataSet.CategoriesRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            categoriesDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte názov!", "Kategórie - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtAlterName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte alternatívny názov!", "Kategórie - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtSex.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte pohlavie!", "Kategórie - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                
                this.drActiveCategory["Active"] = chkActive.Checked;
                this.drActiveCategory["Name"] = txtName.Text;
                this.drActiveCategory["AlterName"] = txtAlterName.Text;
                this.drActiveCategory["CodeName"] = txtCodeName.Text;
                this.drActiveCategory["Sex"] = txtSex.Text;
                this.drActiveCategory["YearFrom"] = nudYearFrom.Value;
                this.drActiveCategory["YearTo"] = nudYearTo.Value;
                this.drActiveCategory["Points"] = txtPoints.Text;
                this.drActiveCategory["Modified"] = DateTime.Now;

                if (bNew)
                {
                    aDbDataSet.Categories.Rows.Add(this.drActiveCategory);
                    this.categoriesTableAdapter.Update(aDbDataSet.Categories);
                }
                else
                {
                    this.categoriesTableAdapter.Update(this.drActiveCategory);
                }

                this.categoriesTableAdapter.Fill(this.aDbDataSet.Categories);

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;
                
                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kategórie - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        



        

        

        
        
    }
}
